﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BookStore.Platform.objects;
using BookStore.Platform.data;
using System.IO;

namespace BookStore.Platform.apis
{
    public class WebSiteApi:IWebSiteApi
    {
        private IConvertApi convertApi = null;
        public WebSiteApi(IConvertApi convertApi)
        {
            this.convertApi = convertApi;
        }
        #region IWebSiteApi Members

        public void Save(WebSite webSite)
        {
            using (BookStoreDataContext context = new BookStoreDataContext()) 
            {
                var entity = context.t_websites.FirstOrDefault(x => x.WebSiteName == webSite.WebSiteName);
                if (entity == null)
                    context.t_websites.InsertOnSubmit(convertApi.convertBizToEntity(webSite));
                else
                {
                    entity.WebSiteUrl = webSite.WebSiteUrlFormat;
                }
                context.SubmitChanges();
            }
            //check folder
            if (!Directory.Exists(webSite.WebSiteName))
            {
                try
                {
                    Directory.CreateDirectory(webSite.WebSiteName);
                }
                catch (Exception e)
                { 
                }
            }
        }

        public WebSite GetByName(string webSiteName)
        {
            using (BookStoreDataContext context = new BookStoreDataContext())
            {
                var entity = context.t_websites.FirstOrDefault(x => x.WebSiteName == webSiteName);
                if (entity == null)
                    return null;
                else
                    return convertApi.convertEntityToBiz(entity);
            }
        }

        public WebSite GetById(Guid webSiteId)
        {
            using (BookStoreDataContext context = new BookStoreDataContext())
            {
                var entity = context.t_websites.FirstOrDefault(x => x.WebSiteId == webSiteId);
                if (entity == null)
                    return null;
                else
                    return convertApi.convertEntityToBiz(entity);
            }
        }

        #endregion

        #region

        //private WebSite convertEntityToBiz(t_website entity)
        //{
        //    WebSite webSite = new WebSite() 
        //    {
        //        WebSiteId = entity.WebSiteId,
        //        WebSiteName = entity.WebSiteName,
        //        WebSiteUrlFormat = entity.WebSiteUrl
        //    };
        //    return webSite;
        //}

        //private t_website convertBizToEntity(WebSite webSite) 
        //{
        //    t_website entity = new t_website() 
        //    {
        //        Created = DateTime.Now,
        //        Updated = DateTime.Now,
        //        WebSiteName = webSite.WebSiteName,
        //        WebSiteId = (webSite.WebSiteId == null)? Guid.Empty : webSite.WebSiteId,
        //        WebSiteUrl = webSite.WebSiteUrlFormat,
        //    };
        //    return entity;
        //}
        #endregion
    }

    public interface IWebSiteApi
    {
        void Save(WebSite webSite);

        WebSite GetByName(string webSiteName);

        WebSite GetById(Guid webSiteId);
    }
}
